﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

using Rubik.Tester;

namespace RubikChallenge
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string assemblyPath = args[0];
                Assembly assembly = Assembly.LoadFrom(assemblyPath);

                RubikSolutionLoader loader = new RubikSolutionLoader();
                RubikSolutionActivator activator = loader.Load(assembly);

                RubikTestManager testManager = new RubikTestManager();

                Console.WriteLine("Testing..");
                IEnumerable<SolutionTestResult> results = testManager.Test(activator);

                int index = 0;
                int result = 0;
                bool isPassed = true;
                foreach (SolutionTestResult solutionTestResult in results)
                {
                    isPassed &= solutionTestResult.Passed;
                    if (solutionTestResult.Passed)
                    {
                        if (solutionTestResult.Length > result)
                        {
                            result = solutionTestResult.Length;
                        }
                        Console.WriteLine(" Test #{0}, Passed, Result = {1} bits",
                            ++index,
                            solutionTestResult.Length);
                    }
                    else
                    {
                        Console.WriteLine(" Test #{0}, Failed, Message: {1}",
                            ++index,
                            solutionTestResult.ErrorMessage);
                    }
                }

                Console.WriteLine("Result:");
                if (isPassed)
                {
                    Console.WriteLine(" Passed, Score: {0}", result);
                }
                else
                {
                    Console.WriteLine(" Not passed");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
